package com.ivan.toolkit.swing;

import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics;
import java.awt.Rectangle;

import javax.swing.Icon;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;

public class ShadowLabel extends JLabel {
	
	boolean innerOpaque = false;
	
	JLabel shadower = new JLabel();
	
	Icon shadowerIcon = new Icon() {

		@Override
		public void paintIcon(Component c, Graphics g, int x, int y) {
		}

		@Override
		public int getIconWidth() {
			Icon icon = getIcon();
			if(icon != null) {
				return icon.getIconWidth();
			}
			return 0;
		}

		@Override
		public int getIconHeight() {
			Icon icon = getIcon();
			if(icon != null) {
				return icon.getIconHeight();
			}
			return 0;
		}
		
	};
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1506886029165035544L;
	
	
	
	

	public ShadowLabel() {
		super();
	}

	public ShadowLabel(Icon image, int horizontalAlignment) {
		super(image, horizontalAlignment);
	}

	public ShadowLabel(Icon image) {
		super(image);
	}

	public ShadowLabel(String text, Icon icon, int horizontalAlignment) {
		super(text, icon, horizontalAlignment);
	}

	public ShadowLabel(String text, int horizontalAlignment) {
		super(text, horizontalAlignment);
	}

	public ShadowLabel(String text) {
		super(text);
	}

	@Override
	protected void paintComponent(Graphics g) {
		
		Icon icon = this.getIcon();
		if(icon != null) {
			shadower.setIcon(shadowerIcon);
		}
		else {
			shadower.setIcon(null);
		}
		
		shadower.setBorder(this.getBorder());
		shadower.setText(this.getText());
		shadower.setFont(this.getFont());
		shadower.setHorizontalAlignment(this.getHorizontalAlignment());
		shadower.setHorizontalTextPosition(this.getHorizontalTextPosition());
		shadower.setVerticalAlignment(this.getVerticalAlignment());
		shadower.setVerticalTextPosition(this.getVerticalTextPosition());
		shadower.setIconTextGap(this.getIconTextGap());
		
		if(innerOpaque) {
			g.setColor(this.getBackground());
			g.fillRect(0, 0, this.getWidth(), this.getHeight());
		}
		
		Rectangle r = this.getBounds();
		r.x = 1;
		r.y = 1;
		SwingUtilities.paintComponent(g, shadower, this, r);

		super.paintComponent(g);
		
	}

	public void setShadow(Color shadow) {
		shadower.setForeground(shadow);
		//this.invalidate();
		//this.repaint();
	}
	
	public Color getShadow() {
		return shadower.getForeground();
	}

	@Override
	public void setOpaque(boolean isOpaque) {
		this.innerOpaque = isOpaque;
		super.setOpaque(false);
	}
	
	
	
}
